package com.sxkf.template.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.entity.Category;
import com.sxkf.template.entity.Product;
import com.sxkf.template.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* (t_category)表控制层
*
* @author zgc
*/
@Api(tags="分类模块")
@RestController
@RequestMapping("/t_category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation("创建分类信息")
    @PostMapping("/createCategory")
    public R<Boolean> createCategory(@RequestBody Category category){
        return R.success(categoryService.save(category));
    }

    @ApiOperation("修改分类信息")
    @PostMapping("/updateCategory")
    public R<Boolean> updateCategory(@RequestBody Category category){
        return R.success(categoryService.updateById(category));
    }

    @ApiOperation(value = "查询所有分类")
    @GetMapping(value = "/page")
    public R<IPage<Category>> page(@RequestParam Integer pageNo,@RequestParam Integer pageSize){
        return R.success(categoryService.page(new Page<>(pageNo,pageSize),new LambdaQueryWrapper<Category>().orderByAsc(Category::getSort)));
    }

    @ApiOperation("查询店铺分类信息")
    @GetMapping("/listCategoryByStoreId")
    public R<List<Category>> listCategoryByStoreId(@RequestParam String storeId){
        return R.success(categoryService.listCategoryByStoreId(storeId));
    }

    @ApiOperation("删除分类信息")
    @PostMapping("/deleteCategory")
    public R<Boolean> deleteCategory(@RequestParam String id){
        return R.success(categoryService.removeById(id));
    }

    @ApiOperation(value = "排行榜")
    @GetMapping(value = "/rank")
    public R<IPage<Category>> rank(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                                  @RequestParam String categoryId){
        IPage<Category> result = categoryService.rank(pageNo,pageSize,categoryId);
        return R.success(result);
    }
}
